#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
This Module is for drawing an Orthogonal Diagrams of the fractures
"""

import sys
from optparse import OptionParser
import fr_parser

    
def readdensity(fname):
    f = open(fname, 'r')	
    lines = f.readlines(); f.close
    result = []
    size = float(lines.pop(0))
    max_density_val = float(lines.pop(0))
    for line in lines:
        valyes = [float(x) for x in line.split()]
        result.append(valyes)
    return result, size, max_density_val



# Parsing of an options
optparser= OptionParser()
optparser.add_option('-o', '--output', dest='output',
                     help='Name of a output file')
optparser.add_option('-t', '--title', dest='title',
                     help='Title for the plot',
                     default='')
optparser.add_option('-c', '--comment', dest='comment',
                     help='Comment to be displaied on a plot',
                     default='')                     
optparser.add_option('-n', dest='count',
                     help='Count the number of the planes and plot it below \
                     the plot',
                     action='store_true',
                     default=False)                     
optparser.add_option('-d', dest='dfile',
					help='Plot the density grid, which store in the file name, on the background')
optparser.add_option('-r', dest='normals',
					help='Plot normals instead of planes', 
					action='store_true',
                    default=False)
optparser.add_option('-i', dest='isolines',
					help='Plot isolines', 
					action='store_false',
                    default=True)
optparser.add_option('-I', dest='pdensity',
					help='Plot density', 
					action='store_false',
                    default=True) 
optparser.add_option('-b', dest='blank',
					help='Blank mode. No title, no annotation, no comments', 
					action='store_true',
                    default=False)
optparser.add_option('-W', dest='width',
					help='Width of the plot', type='int' ,
                    default=800)
optparser.add_option('-H', dest='height', type='int',
					help='Height of the plot', 
                    default=600) 
optparser.add_option('-w', dest='whitespaces', type='float',
                    help='Whitespaces for title and annotation',
                    default=1.4)
optparser.add_option('-y', dest='normals_hard',
					help='Plot normals instead of planes (hard)', 
					action='store_true',
                    default=False)				
optparser.add_option('-k', dest='allTR',
					help='Allways read inpit directions as TR when', 
					action='store_true',
                    default=False)				
                                                                                                               
options, args = optparser.parse_args(sys.argv[1:])
file_name= options.output
lines = sys.stdin.read().split('\n')

# Parse the density grid file
density, size, max_density_val = None, None, None
if options.dfile:
	density, size, max_density_val = readdensity(options.dfile)

# Parsig of a data file
data=[]
for line in lines:
    val = fr_parser.parse_line(line)
    if val: data.append(val)    

# Guuessing a command sequenses to plot the diagramm
import orth_canvas
commands = orth_canvas.ReturnCommands(data, options.normals, options.normals_hard, options.allTR)

count=0
if options.count:
    count=len(data)

# Plotting
import orth_cairo_file
orth_cairo_file.plot_diagram(commands, file_name, [options.width,options.height],
    options.title, options.comment, count, density, size, max_density_val, 
                        options.isolines, options.pdensity, options.blank,
                        options.whitespaces)
